﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Controller;

public class IAViewModel : MonoBehaviour {
	[SerializeField]
	public List<GameObject> _commonNPC;
	
	private List<CitizenModel> listCommonNPC;
	private PrNPCController controllerNPC;
    private ListPlaces listPlaces = null;

	void Start () {
        listPlaces = new ListPlaces();
		listCommonNPC = new List<CitizenModel>();
		controllerNPC = new PrNPCController();
		for(int i=0;i<_commonNPC.Count;i++){
			listCommonNPC.Add(((GameObject)_commonNPC[i]).GetComponent<CitizenModel>() );
            controllerNPC.SetListPlaces(listPlaces);
			controllerNPC.StartPath(listCommonNPC[i]);
		}
	}

    Vector3 pos = Vector3.zero;
    int range = 0;
	void LateUpdate () {
            for (int i = 0; i < listCommonNPC.Count; i++)
            {
                controllerNPC.MoveNPC(listCommonNPC[i]);
                if (Time.frameCount % 30 == 0)
                {
                    if (ScreenGeneral.MaxThrow <= 20)
                    {
                        if (listCommonNPC[i].GetCitizen().WaitTime == 0)
                        {
                            range = Random.Range(0, 100);
                            if (range > 20 && range < 30)
                            {
                                pos.x = listCommonNPC[i].GetModelCitizen().transform.position.x;
                                pos.z = listCommonNPC[i].GetModelCitizen().transform.position.z;
                                pos.y = 4;
                                Instantiate(listCommonNPC[i].GetModelElement(), pos, listCommonNPC[i].GetModelCitizen().transform.rotation);
                                ScreenGeneral.MaxThrow++;
                            }
                            listCommonNPC[i].GetCitizen().WaitTime = 10;
                        }
                        else
                            listCommonNPC[i].GetCitizen().WaitTime--;
                    }
                }
            }
        
	}

}
